package com.molichuxing.services.util;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.molichuxing.services.sdk.py.Base64;
import com.molichuxing.services.sdk.py.PyUtils;
import com.molichuxing.services.sdk.py.request.Conditions;
import com.molichuxing.services.sdk.py.request.Items;
import com.molichuxing.services.sdk.py.request.QueryCondition;

public class PyParamsUtil {

	    private static Logger logger = Logger.getLogger(PyParamsUtil.class);
	
	    public static String requestApi(String host, String path,String userId,String password,String param) throws Exception {

	        // https双向认证使用,配合PySSLContextUtil中的DefaultSSLContext
//	        System.setProperty("javax.net.debug", "all");
//	        System.setProperty("javax.net.ssl.keyStore",keystoreFile);
//	        System.setProperty("javax.net.ssl.keyStorePassword", keystorePassword);
//	        System.setProperty("javax.net.ssl.trustStore", keystoreFile);
//	        System.setProperty("javax.net.ssl.trustStorePassword", keystorePassword);

	        Map<String, String> headers = new HashMap<String, String>();
	        Map<String, String> querys = null;
	        Map<String, String> bodys = new HashMap<String, String>();
			//默认请求条件是JSON格式.如果请求条件是xml，需要指定格式。
			//bodys.put("format","xml")
	        bodys.put("userID",userId);
	        bodys.put("password",password);
	        bodys.put("queryCondition",param);
	        HttpResponse response = PyHttpUtils.doPost(host, path, headers, querys, bodys);
	        String result = EntityUtils.toString(response.getEntity());
	        return result;
	    }

		public static String requestApiZip(String host, String path,String userId,String password,String param) throws Exception {

	        // https双向认证使用,配合PySSLContextUtil中的DefaultSSLContext
//	        System.setProperty("javax.net.debug", "all");
//	        System.setProperty("javax.net.ssl.keyStore",keystoreFile);
//	        System.setProperty("javax.net.ssl.keyStorePassword", keystorePassword);
//	        System.setProperty("javax.net.ssl.trustStore", keystoreFile);
//	        System.setProperty("javax.net.ssl.trustStorePassword", keystorePassword);

	        Map<String, String> headers = new HashMap<String, String>();
	        Map<String, String> querys = null;
	        Map<String, String> bodys = new HashMap<String, String>();
			//默认请求条件是JSON格式.如果请求条件是xml，需要指定格式。
			//bodys.put("format","xml")
	        bodys.put("userID",userId);
	        bodys.put("password",password);
	        bodys.put("queryCondition",param);
	        bodys.put("returnStyle","json;pdf");
	        HttpResponse response = PyHttpUtils.doPost(host, path, headers, querys, bodys);
	        String result = EntityUtils.toString(response.getEntity());
	        Map<String,Object> resultMap = JSON.parseObject(result);
	        String status=(String)resultMap.get("status");
	        if("1".equals(status)) {
	        	String returnValue= (String)resultMap.get("returnValue");
	        	logger.info("鹏元返回第三方数据:"+returnValue);
	        	byte[] data =new Base64().decode(returnValue);
	        	FileOutputStream fos = new FileOutputStream("/mnt/result.zip");
	        	fos.write(data);
	        	fos.flush();
	        	fos.close();
	        	return PyUtils.decodeAndDecompress(returnValue, "UTF-8");
	        }else {
	        	return result;
	        }
	        
	    }
	    
	    /**
	     * 征信参数封装
	     * @param userId
	     * @param password
	     * @param userName
	     * @param cardNo
	     * @param photo
	     * @return
	     * @throws Exception
	     */
	    public static String getCreditParam(String userName,String cardNo,String phone){
	        QueryCondition qc = new QueryCondition();
	        List<Conditions> conditions =new ArrayList<Conditions>();
	        
	        Conditions cons = new Conditions();
	        
	        List<Items> items = new ArrayList<Items>();
	        
	        Items it1 = new Items();
	        it1.setName("name");
	        it1.setValue(userName);
	        items.add(it1);

	        Items it2 = new Items();
	        it2.setName("documentNo");
	        it2.setValue(cardNo);
	        items.add(it2);

	        Items it3 = new Items();
	        it3.setName("phone");
	        it3.setValue(phone);
	        items.add(it3);

	        //96040   个人反欺诈
	        Items it4 = new Items();
	        it4.setName("subreportIDs");
	        it4.setValue("96040");
	        items.add(it4);

	        //101	贷款审批
	        Items it5 = new Items();
	        it5.setName("queryReasonID");
	        it5.setValue("101");
	        items.add(it5);
	        
	        
	        cons.setItems(items);
	        //25212	个人反欺诈
	        cons.setQueryType("25212");
	        conditions.add(cons);
	        qc.setConditions(conditions);
	        return JSON.toJSONString(qc);
	    }
	    
	    
	    /**
	     * 身份认证参数封装
	     * @param userId
	     * @param password
	     * @param userName
	     * @param cardNo
	     * @param photo
	     * @return
	     * @throws Exception
	     */
	    public static String getIdentityParam(String userName,String  cardNo,String photo){
	        QueryCondition qc = new QueryCondition();
	        List<Conditions> conditions =new ArrayList<Conditions>();
	        
	        Conditions cons = new Conditions();
	        
	        List<Items> items = new ArrayList<Items>();
	        
	        Items it1 = new Items();
	        it1.setName("name");
	        it1.setValue(userName);
	        items.add(it1);

	        Items it2 = new Items();
	        it2.setName("documentNo");
	        it2.setValue(cardNo);
	        items.add(it2);

	        Items it3 = new Items();
	        it3.setName("photo");
	        it3.setValue(photo);
	        items.add(it3);

	        //14600	人脸身份验证
	        Items it4 = new Items();
	        it4.setName("subreportIDs");
	        it4.setValue("14600");
	        items.add(it4);

	        //101	贷款审批
	        Items it5 = new Items();
	        it5.setName("queryReasonID");
	        it5.setValue("101");
	        items.add(it5);
	        
	        
	        cons.setItems(items);
	        //25191	人脸照片比对_主机对主机查询
	        cons.setQueryType("25191");
	        conditions.add(cons);
	        qc.setConditions(conditions);
	        return JSON.toJSONString(qc);
	    }
	    
	    /**
	     * 创建鹏元驾驶证接口参数对象
	     * @param userId
	     * @param password
	     * @param userName
	     * @param cardNo
	     * @return
	     * @throws Exception
	     */
	    public static String getDrivingLicense(String userName,String cardNo){
	        QueryCondition qc = new QueryCondition();
	        List<Conditions> conditions =new ArrayList<Conditions>();
	        
	        List<Items> items = new ArrayList<Items>();
	        Conditions cons = new Conditions();
	        Items it = new Items();
	        it.setName("name");
	        it.setValue(userName);
	        items.add(it);
	        
	        Items it1 = new Items();
	        it1.setName("documentNo");
	        it1.setValue(cardNo);
	        items.add(it1);

	        Items it2 = new Items();
	        it2.setName("subreportIDs");
	        it2.setValue("14909");
	        items.add(it2);

	        Items it3 = new Items();
	        it3.setName("queryReasonID");
	        it3.setValue("101");
	        items.add(it3);

	        Items it4 = new Items();
	        it4.setName("refID");
	        it4.setValue("");
	        items.add(it4);
	       
	       
	        cons.setItems(items);
	        cons.setQueryType("25136");
	        conditions.add(cons);
	        qc.setConditions(conditions);
	       
	        return JSON.toJSONString(qc);
	    }

	    
	    /**
	     * 汽车金融参数封装
	     * @param userId
	     * @param password
	     * @param userName
	     * @param cardNo
	     * @param photo
	     * @return
	     * @throws Exception
	     */
	    public static String getFinanceParam(String userName,String  cardNo,String phone){
	        QueryCondition qc = new QueryCondition();
	        List<Conditions> conditions =new ArrayList<Conditions>();
	        
	        Conditions cons = new Conditions();
	        
	        List<Items> items = new ArrayList<Items>();
	        
	        Items it1 = new Items();
	        it1.setName("name");
	        it1.setValue(userName);
	        items.add(it1);

	        Items it2 = new Items();
	        it2.setName("documentNo");
	        it2.setValue(cardNo);
	        items.add(it2);

	        Items it3 = new Items();
	        it3.setName("phone");
	        it3.setValue(phone);
	        items.add(it3);

	        //15140	汽车金融
	        Items it4 = new Items();
	        it4.setName("subreportIDs");
	        it4.setValue("15140");
	        items.add(it4);

	        //101	贷款审批
	        Items it5 = new Items();
	        it5.setName("queryReasonID");
	        it5.setValue("101");
	        items.add(it5);
	        
	        
	        cons.setItems(items);
	        //25136	汽车金融
	        cons.setQueryType("25136");
	        conditions.add(cons);
	        qc.setConditions(conditions);
	        return JSON.toJSONString(qc);
	    }

		
}
